<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<%@ include file="/common/common.jsp" %>
<%
    String[] types = new String[]{
            "材料及燃动力",    //1
            "研发人员费用",    //2
            "中间试验和产品试制费人员费用",      //3
            "折旧费用",      //4
            "固定资产租赁费",    //5
            "无形资产摊销费",     //6
            "中间试验和产品试制费",    //7
            "样品成本转出",//8
            "论证鉴定费",      //9
            "委外费用",        //10
            "其他科研费用"};      //11
    String prjid = request.getParameter("prjid");
    JdbcDao dao = Env.getBean("jdbcDao");
    String year = DateUtil.getCurrentDate().substring(0,4);
    //总预算
    Map t_values=new HashMap();
    List<Map> t_list=dao.getJdbcTemplate().queryForList("select * from ut_prjbudget where prjid=?",new Object[]{prjid});
    for(Map row:t_list){
        t_values.put(row.get("kemu"),row.get("val"));
    }
    //年度预算
    Map y_values=new HashMap();
    List<Map> y_list=dao.getJdbcTemplate().queryForList("select * from ut_prjbudgetyear where prjid=? and lf=?",new Object[]{prjid,year});
    for(Map row:y_list){
        y_values.put(row.get("kemu"),row.get("val"));
    }
%>
<html>
<head>
    <%@ include file="/common/head.jsp"%>
    <style>

        table.datagrid2 {
            border-collapse: collapse;
        }

        table.datagrid2 th {
            text-align: left;
            background: #9cf;
            padding: 3px;
            border: 1px #333 solid;
        }

        table.datagrid2 td {
            padding: 3px;
            border: none;
            border: 1px #333 solid;
        }
    </style>
    <script language="javascript">
        var topBar = null;
        Ext.onReady(function() {
            topBar = new Ext.Toolbar();
            topBar.render('pagemenubar');
            topBar.addSeparator();
            addBtn(topBar, '保存', 'S', 'accept', saveBudget);
            addBtn(topBar, '导入', 'I', 'accept', importBudget);
            cal();
            cal1();
        });

        function saveBudget() {
            Ext.Ajax.request({
                url: 'budgetop.jsp',
                form:mainform,
                isUpload:false,
                success: function() {
                    pop("保存成功!");
                },
                params: { action:'savebudget' }
            });
        }

        function importBudget() {
            $('#fileform').fadeIn();
        }
        function doImport(btn){
            btn.disabled=true;
            Ext.Ajax.request({
                url: 'budgetop.jsp',
                form:fileform,
                isUpload:true,
                success: function() {
                    pop("导入成功!");
                    location.reload();
                },
                params: { action:'import' }
            });
        }
    </script>
</head>
<body>
<div id='pagemenubar'></div>
<form id='fileform' style="display:none">
    <div><a href="/zbc/template.xls">导入模板下载</a></div>
    <div>请选择导入文件：</div>
    <input type='file' name='excelfile'>
    <input type='hidden' name="prjid" value="<%=prjid%>">
    <input type='button' onclick="doImport(this)" value="处理">
</form>
<form id='mainform'>
    <input type='hidden' name="prjid" value="<%=prjid%>">
    <input type='hidden' name='year' value="<%=year%>">
    <table class='datagrid2'>
        <colgroup>
            <col width="30%"/>
            <col width="30%"/>
            <col width="20%"/>
            <col width="20%"/>
        </colgroup>
        <tr>
            <th>科目类别</th>
            <th>科目</th>
            <th>总预算</th>
            <th>年度预算</th>
        </tr>
        <%
            for (int i = 0; i < types.length; i++) {
                String typename = types[i];
                List<Map> kemulist = dao.getJdbcTemplate().queryForList("select * from ut_fyys where kmtype=? order by orderby", new Object[]{i + 1});
                int rowspan = kemulist.size();
                for (int j = 0; j < rowspan; j++) {
                    Map kemu = kemulist.get(j);
                    if (j == 0) {
        %>
        <tr>
            <td rowspan="<%=rowspan%>"><%=typename%>
            </td>
            <td><%=kemu.get("km1")%>
            </td>
            <td><input name='t_<%=kemu.get("processid")%>' onblur="fieldcheck(this,'^(-?\\d+)(\\.\\d+)?$','总预算');cal();" value="<%=StringUtil.null2String(t_values.get(kemu.get("processid")))%>">
            </td>
            <td><input name='y_<%=kemu.get("processid")%>'
                       onblur="fieldcheck(this,'^(-?\\d+)(\\.\\d+)?$','年度预算');cal1();" value="<%=StringUtil.null2String(y_values.get(kemu.get("processid")))%>"></td>
        </tr>
        <%
        } else {
        %>
        <tr>
            <td><%=kemu.get("km1")%>
            </td>
            <td><input name='t_<%=kemu.get("processid")%>' onblur="fieldcheck(this,'^(-?\\d+)(\\.\\d+)?$','总预算');cal();" value="<%=StringUtil.null2String(t_values.get(kemu.get("processid")))%>">
            </td>
            <td><input name='y_<%=kemu.get("processid")%>'
                       onblur="fieldcheck(this,'^(-?\\d+)(\\.\\d+)?$','年度预算');cal1();" value="<%=StringUtil.null2String(y_values.get(kemu.get("processid")))%>"></td>
        </tr>
        <%
                    }
                }
            }
        %>
        <tr>
            <td>合计</td>
            <td></td>
            <td><span id='total'></span></td>
            <td><span id='total1'></span></td>
        </tr>
    </table>
</form>
<script type="text/javascript">

    function cal() {
        var total = 0;
        $('input[name^="t_"]').each(function() {

            total = total + (isNaN(parseFloat(this.value)) ? 0 : parseFloat(this.value));
        })
        document.getElementById("total").innerHTML = total.toFixed(2);
    }
    function cal1() {
        var total = 0;
        $('input[name^="y_"]').each(function() {
            total = total + (isNaN(parseFloat(this.value)) ? 0 : parseFloat(this.value));
        })
        document.getElementById("total1").innerHTML = total.toFixed(2);
    }
</script>
</body>
</html>